package chapter15.Exercise;

import java.util.*;

/**
 * @Auther: Tx
 * @Date: 2022/5/26 20:55
 */
public class Ex01 {
    public static void main(String[] args) {
//
        HashMap<String, Student> Map = new HashMap<>();
        Map.put("tom",new Student("1"));
        Map.put("jack",new Student("2"));
//        1.第一种遍历方式，增强for循环,和keySet遍历
        Set<String> set = Map.keySet();
        for (String name:set) {
            System.out.println(Map.get(name));
        }
//        2.第二种遍历方式
        Set<java.util.Map.Entry<String, Student>> entries = Map.entrySet();
        Iterator<java.util.Map.Entry<String, Student>> iterator1 = entries.iterator();
        while (iterator1.hasNext()) {
            Map.Entry next = iterator1.next();
            System.out.println(next);

        }
//使用泛型给HashSet添加对象
        HashSet<Student> students = new HashSet<>();
        students.add(new Student("3"));
        students.add(new Student("4"));
//        迭代器遍历
        Iterator<Student> iterator = students.iterator();
        while (iterator.hasNext()) {
            Student next =  iterator.next();
            System.out.println(next);
        }
        //    增强for循环
        for (Student student: students) {
            System.out.println(student);
        }
    }


}
class Student{
     private String id;

    public Student(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                '}';
    }
}